#include <cstdio>
#include <cstring>
#include <iostream>
const int N = 205;
using namespace std;
int n, m, sx, sy, K, ans, dp[N][N];
const int dx[5] = {0, -1, 1, 0, 0};
const int dy[5] = {0, 0, 0, -1, 1};
struct node {
  int dp, pos;
} q[N];
char map[N][N];
void work(int x, int y, int len, int d) {
  int head = 1, tail = 0;
  for (int i = 1; x >= 1 && x <= n && y >= 1 && y <= m;
       i++, x += dx[d], y += dy[d])
    if (map[x][y] == 'x')
      head = 1, tail = 0;
    else {
      while (head <= tail && q[tail].dp + i - q[tail].pos < dp[x][y]) tail--;
      q[++tail] = node{dp[x][y], i};
      if (q[tail].pos - q[head].pos > len) head++;
      dp[x][y] = q[head].dp + i - q[head].pos;
      ans = max(ans, dp[x][y]);
    }
}
int main() {
  scanf("%d%d%d%d%d", &n, &m, &sx, &sy, &K);
  for (int i = 1; i <= n; i++) scanf("%s", map[i] + 1);
  memset(dp, 0xf3, sizeof(dp));
  dp[sx][sy] = 0;
  for (int k = 1, s, t, d, len; k <= K; k++) {
    scanf("%d%d%d", &s, &t, &d);
    len = t - s + 1;
    if (d == 1)
      for (int i = 1; i <= m; i++) work(n, i, len, d);
    if (d == 2)
      for (int i = 1; i <= m; i++) work(1, i, len, d);
    if (d == 3)
      for (int i = 1; i <= n; ++i) work(i, m, len, d);
    if (d == 4)
      for (int i = 1; i <= n; ++i) work(i, 1, len, d);
  }
  printf("%d\n", ans);
  return 0;
}
